<template>
  <div class="app-container calendar-list-container">

    <div class="filter-container" style="padding-bottom: 10px;display: flex"><!-- 上边的搜索条件 -->
      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px;"
        class="filter-item"
        placeholder="报价单审批环节" v-model="listQuery.query.approve_step">
      </el-input>
      <el-select v-model="listQuery.query.cst_company_id"
                 filterable
                 multiple
                 remote
                 reserve-keyword
                 :remote-method="remoteMethod"
                 :loading="companySearchLoading"
                 placeholder="客户公司(可多选)">
        <el-option
          v-for="item in options_search"
          :key="item.cst_company_id"
          :label="item.cst_company_name"
          :value="item.cst_company_id">
        </el-option>
      </el-select>

      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px;"
        class="filter-item"
        placeholder="产品名称" v-model="listQuery.query.prod_name">
      </el-input>
      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px;"
        class="filter-item"
        placeholder="报价单号" v-model="listQuery.query.quote_no">
      </el-input>
      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px;"
        class="filter-item"
        placeholder="业务员" v-model="listQuery.query.salesman">
      </el-input>
      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px;"
        class="filter-item"
        placeholder="开始日期" v-model="listQuery.query.start_date">
      </el-input>
      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px;"
        class="filter-item"
        placeholder="结束日期" v-model="listQuery.query.end_date">
      </el-input>
      <el-select v-model="listQuery.query.type" multiple placeholder="报价单类型(可多选)">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
      <!--<el-button class="filter-item" type="primary" icon="document" @click="handleDownload">导出</el-button>-->
    </div>

    <!-- 展示数据的table -->
    <el-table
      :data="list"
      v-loading.body="listLoading"
      element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" label="报价单id" max-width="250px">
        <template scope="scope">
          <span>{{scope.row.quote_id}}</span>
        </template>
      </el-table-column>

      <el-table-column max-width="250px" align="center" label="客户名称">
        <template scope="scope">
          <span>{{scope.row.cst_name}}</span>
        </template>
      </el-table-column>

      <el-table-column max-width="250px" align="center" label="报价单编号">
        <template scope="scope">
          <span>{{scope.row.quote_no}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" max-width="250px">
        <template scope="scope">
          <el-button type="primary"
                     @click='toEdit(scope.row.quote_id)'
                     size="small" icon="edit">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            @click="delRow(scope.row.quote_id)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <div v-show="!listLoading" class="pagination-container" style="margin-top: 30px;">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.curr_page"
                     :page-sizes="[1, 2, 3, 5]" :page-size="listQuery.page_size" layout="total, sizes, prev, pager, next, jumper" :total="total_count">
      </el-pagination>
    </div>

  </div>
</template>

<script>
  import { quoteList,deleteQuote } from '@/api/cstOrder/quote'
  import { cstSupSearch } from '@/api/enterprise/supply'

  export default {
    name: 'quoteList',
    data() {
      return {
        list: null,
        listLoading: true,
        total_count:null,//数据总条数
        total_page:null,//总页数
        companySearchLoading:false,
        listQuery: {
          client_type:1,
          company_id:'',
          curr_page: 1,
          page_size: 2,
          query:{
            approve_step:null,//审批状态: 10起草中，20复核，30审批，0审批结束
            cst_company_id:[],
            end_date:null,
            prod_name:null,
            quote_no:null,
            salesman:null,
            start_date:null,
            type:[],
          }
        },
        delData:{
          client_type:1,
          quote_id:''
        },
        options: [
          {
            value: 'step10',
            label: '起草'
          },
          {
            value: 'step11',
            label: '退回起草'
          },
          {
            value: 'step20',
            label: '复核'
          },
          {
            value: 'step30',
            label: '审批'
          },
          {
            value: 'step0',
            label: '审批结束'
          }
        ],
        options_search:[],
        searchReqData:{
          client_type:1,
          cst_sup_type:1,//1:company 2:user
          curr_page:1,
          page_size:10
        }
      }
    },
    mounted() {
      this.getList()

    },
    methods: {
      getList() {
        this.listLoading = true
        quoteList(this.listQuery).then(res => {
          this.list = res.quote_list
          this.total_count = res.total_count
          this.total_page = res.total_page
          this.listLoading = false
        })
      },
      handleFilter() {
        this.listQuery.curr_page = 1
        this.getList()
      },
      handleSizeChange(val) {
        this.listQuery.page_size = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.curr_page = val
        this.getList()
      },
      delRow(id){
        this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delData.quote_id = id
          deleteQuote(this.delData)
            .then(res => {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.listQuery.curr_page = 1
              this.getList()
            })
            .catch(err => {
              this.$message.error('删除失败!');
              console.log(err);
            })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      toEdit(id){
        this.$router.push({path:'createQuote',query:{quote_id:id}})
      },
      remoteMethod(query) {
        this.companySearchLoading = true;
        this.searchReqData.cst_sup_name = query
        cstSupSearch(this.searchReqData)
          .then(res => {
            setTimeout(() => {
              this.companySearchLoading = false;
              this.options_search = res.cst_sup
            }, 1000 * Math.random());
          })
          .catch(err => {
            this.$message.error('查询失败!');
            console.log(err);
          })
      }
    }
  }
</script>

<style lang="scss">
  .my-autocomplete {
    li {
      line-height: normal;
      padding: 7px;

      .name {
        text-overflow: ellipsis;
        overflow: hidden;
      }

      .highlighted .addr {
        color: #ddd;
      }
    }
  }
</style>
